//
//  huaZhuanViewController.m
//  gpos_app
//
//  Created by apple on 2018/8/24.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "huaZhuanViewController.h"
#import "Masonry.h"
#import "XMRichScanViewController.h"
#import "GPosAPI.h"
#import "EBCountDownButton.h"
#import "SVProgressHUD.h"
#import "WKProgressHUD.h"
#import "XMRegisterViewController.h"

#define NUM @"0123456789."
#define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz."
#define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789."


@interface huaZhuanViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UIView * siteView;
@property (nonatomic,strong)UIView * shuluangView;
@property (nonatomic,strong)UIView * jymimaView;
@property (nonatomic,strong)UIView * yanzhengmaView;
@property (nonatomic,strong)UIView * phoneView;
@property (nonatomic,strong)UITextField * shuliangTextField;
@property (nonatomic,strong)UITextField * jymimaTextField;
@property (nonatomic,strong)UIButton * yanjingButton;
@property (nonatomic,strong)EBCountDownButton * yanzhengmaButton;
@property (nonatomic,strong)UITextField * yanzhengmaTextField;
@property (nonatomic,strong)UIButton * siteButton;
@property (nonatomic,strong)UITextField * siteTextField;
@property (nonatomic,strong)UILabel * phonetextlabel;
@property(strong,nonatomic)WKProgressHUD *hud;
@property (nonatomic,assign)BOOL isHaveDian;

@property (nonatomic,strong)NSString *yqsign;
@property (nonatomic,strong)NSString *posid;
@property (nonatomic,strong)NSString * DorderNumber;
@property (nonatomic,strong)UIButton * querenButton;
//@property (nonatomic,strong)


@end

@implementation huaZhuanViewController


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden=YES;
    //改变状态栏
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    if ([self.type isEqualToString:@"1"]) {
        NSLog(@"App");
        self.siteTextField.text = self.stringValue;
        self.siteTextField.enabled = YES;
        self.shuliangTextField.enabled = YES;
        
    }else if ([self.type isEqualToString:@"2"]) {
        self.siteTextField.text = self.addr;
        self.shuliangTextField.text = [NSString stringWithFormat:@"%@",@(self.number.floatValue)];
        self.shuliangTextField.enabled = NO;
        self.siteTextField.enabled = NO;
        self.yqsign = self.sign;
        self.posid = self.posId;
        self.DorderNumber = self.orderNumber;
        NSLog(@"pos");
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
     [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.tintColor=[UIColor blackColor];
//    self.tabBarController.tabBar.hidden = YES;
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0f green:244/255.0f blue:244/255.0f alpha:1];
    self.title = @"发送";
    
   
    self.siteView = [[UIView alloc] init];
    self.siteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.siteView];
    
    self.shuluangView = [[UIView alloc] init];
    self.shuluangView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.shuluangView];
    if (iPhoneX) {
        [self.siteView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(kNavbarHeight+10);
            make.left.equalTo(self.view).with.offset(0);
            make.right.equalTo(self.view).with.offset(0);
            make.height.equalTo(@50);
        }];
        
    }else {
        [self.siteView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(74);
            make.left.equalTo(self.view).with.offset(0);
            make.right.equalTo(self.view).with.offset(0);
            make.height.equalTo(@50);
        }];
        
    }
    
    [self.shuluangView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.siteView.mas_bottom).with.offset(1);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.height.equalTo(@50);
    }];
    
    self.jymimaView = [[UIView alloc] init];
    self.jymimaView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.jymimaView];
    [self.jymimaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.shuluangView.mas_bottom).with.offset(42);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.height.equalTo(@50);
    }];
    
    self.phoneView = [[UIView alloc] init];
    self.phoneView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.phoneView];
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.jymimaView.mas_bottom).with.offset(1);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.height.equalTo(@50);
    }];
    
    self.yanzhengmaView = [[UIView alloc] init];
    self.yanzhengmaView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.yanzhengmaView];
    [self.yanzhengmaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneView.mas_bottom).with.offset(1);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.height.equalTo(@50);
    }];
    
    UILabel * sjNamberlabel = [[UILabel alloc] init];
    sjNamberlabel.text = @"余额";
    sjNamberlabel.textColor = [UIColor grayColor];
    sjNamberlabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:sjNamberlabel];
    [sjNamberlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(15);
        make.top.equalTo(self.shuluangView.mas_bottom).with.offset(12);
    }];
    
    UILabel * sjNbrtextlabel = [[UILabel alloc] init];
    sjNbrtextlabel.textColor = [UIColor grayColor];
    sjNbrtextlabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:sjNbrtextlabel];
    [sjNbrtextlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sjNamberlabel.mas_right).with.offset(16);
        make.top.equalTo(self.shuluangView.mas_bottom).with.offset(12);
    }];
    
    UILabel * phonelabel = [[UILabel alloc] init];
    phonelabel.text = @"手机号码";
    phonelabel.textColor = [UIColor blackColor];
    phonelabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:phonelabel];
    [phonelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneView).with.offset(15);
        //        make.top.equalTo(self.jymimaView.mas_bottom).with.offset(12);
        make.centerY.equalTo(self.phoneView);
    }];
    
    self.phonetextlabel = [[UILabel alloc] init];
    self.phonetextlabel.textColor = [UIColor blackColor];
    self.phonetextlabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.phonetextlabel];
    [self.phonetextlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phonelabel.mas_right).with.offset(16);
        make.centerY.equalTo(self.phoneView);
    }];
    
    self.querenButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.querenButton];
    [self.querenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.yanzhengmaView.mas_bottom).with.offset(60);
        make.centerX.equalTo(self.view);
        
    }];
    [self.querenButton setBackgroundImage:[UIImage imageNamed:@"icon_anniu_f"] forState:UIControlStateNormal];
    self.querenButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.querenButton setTitle:@"确认发送" forState:UIControlStateNormal];
    
    [self.querenButton addTarget:self action:@selector(querenButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.phonetextlabel.text = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"]];
//    NSNumber * nsNumber1 = @(self.yue);
    sjNbrtextlabel.text =  [NSString stringWithFormat:@"%@", self.yue];
    [self siteLoadView];
    [self shuliangLoadView];
    [self jymimaLoadView];
    [self yanzhengmaLoadView];
    
    
}

- (void)siteLoadView {
    
    
    UILabel * siteLabel = [[UILabel alloc] init];
    siteLabel.text = @"对方地址";
    siteLabel.font = [UIFont systemFontOfSize:14];
    [self.siteView addSubview:siteLabel];
    [siteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.siteView);
        make.left.equalTo(self.siteView).with.offset(15);
        make.width.equalTo(@60);
    }];
    
    self.siteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.siteView addSubview:self.siteButton];
    [self.siteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.siteView);
        make.right.equalTo(self.siteView).with.offset(-15);
        
    }];
    [self.siteButton setImage:[UIImage imageNamed:@"icon_sys_h"] forState:UIControlStateNormal];
    [self.siteButton addTarget:self action:@selector(siteButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.siteTextField = [[UITextField alloc] init];
    [self.siteView addSubview:self.siteTextField];
    [self.siteTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.siteView);
        make.left.equalTo(siteLabel.mas_right).with.offset(15);
        make.width.equalTo(@200);
//        make.right.equalTo(self.siteButton.mas_left).with.offset(-15);
        
    }];
//    self.siteTextField.secureTextEntry = YES;
    self.siteTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.siteTextField.placeholder =@"请输入对方钱包地址";
    self.siteTextField.font = [UIFont systemFontOfSize:14.f];
    
    
    
}

- (void)shuliangLoadView {
    UILabel * shuliangLabel = [[UILabel alloc] init];
    shuliangLabel.text = @"数量";
    shuliangLabel.font = [UIFont systemFontOfSize:14];
    [self.shuluangView addSubview:shuliangLabel];
    [shuliangLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.shuluangView);
        make.left.equalTo(self.shuluangView).with.offset(15);
        make.width.equalTo(@60);
    }];
    
    UILabel * GPOSLabel = [[UILabel alloc] init];
    GPOSLabel.text = @"GPOS";
    GPOSLabel.font = [UIFont systemFontOfSize:14];
    [self.shuluangView addSubview:GPOSLabel];
    [GPOSLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.shuluangView);
        make.width.equalTo(@40);
        make.right.equalTo(self.shuluangView).with.offset(-15);
    }];
    
    self.shuliangTextField = [[UITextField alloc] init];
    self.shuliangTextField.keyboardType=UIKeyboardTypeDecimalPad;
    [self.shuluangView addSubview:self.shuliangTextField];
    [self.shuliangTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.shuluangView);
        make.left.equalTo(shuliangLabel.mas_right).with.offset(15);
        make.right.equalTo(GPOSLabel.mas_left).with.offset(-15);
        
    }];
    self.shuliangTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.shuliangTextField.placeholder =@"请输入GPOS数量";
    self.shuliangTextField.font = [UIFont systemFontOfSize:14.f];
    self.shuliangTextField.delegate = self;
    
}

- (void)jymimaLoadView {
    
    UILabel * jymiomaLabel = [[UILabel alloc] init];
    jymiomaLabel.text = @"交易密码";
    jymiomaLabel.font = [UIFont systemFontOfSize:14];
    [self.jymimaView addSubview:jymiomaLabel];
    [jymiomaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.jymimaView);
        make.left.equalTo(self.jymimaView).with.offset(15);
        make.width.equalTo(@60);
    }];
    
    self.yanjingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.jymimaView addSubview:self.yanjingButton];
    [self.yanjingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.jymimaView);
        make.right.equalTo(self.jymimaView).with.offset(-15);
        
    }];
    [self.yanjingButton setImage:[UIImage imageNamed:@"icon_biyan_h"] forState:UIControlStateNormal];
    [self.yanjingButton addTarget:self action:@selector(jymimaButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.jymimaTextField = [[UITextField alloc] init];
    self.jymimaTextField.keyboardType=UIKeyboardTypeNumberPad;
    [self.jymimaView addSubview:self.jymimaTextField];
    [self.jymimaTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.jymimaView);
        make.left.equalTo(jymiomaLabel.mas_right).with.offset(15);
        make.right.equalTo(self.yanjingButton.mas_left).with.offset(-15);
        
    }];
    self.jymimaTextField.secureTextEntry = YES;
    self.jymimaTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.jymimaTextField.placeholder =@"请输入交易密码";
    self.jymimaTextField.font = [UIFont systemFontOfSize:14.f];
   
    self.jymimaTextField.delegate = self;
    
    
    
}

- (void)yanzhengmaLoadView {
    
    UILabel * yanzhengmaLabel = [[UILabel alloc] init];
    yanzhengmaLabel.text = @"验证码";
    yanzhengmaLabel.font = [UIFont systemFontOfSize:14];
    [self.yanzhengmaView addSubview:yanzhengmaLabel];
    [yanzhengmaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.yanzhengmaView);
        make.left.equalTo(self.yanzhengmaView).with.offset(15);
        make.width.equalTo(@60);
    }];
    
    self.yanzhengmaButton = [EBCountDownButton buttonWithType:UIButtonTypeCustom];
    [self.yanzhengmaView addSubview:self.yanzhengmaButton];
    [self.yanzhengmaButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.yanzhengmaView);
        make.right.equalTo(self.yanzhengmaView).with.offset(-15);
        make.width.equalTo(@80);
        
    }];
    [self.yanzhengmaButton setTitle:@"发送验证码" forState:UIControlStateNormal];
    [self.yanzhengmaButton setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];
    self.yanzhengmaButton.titleLabel.font = [UIFont systemFontOfSize: 15.0];
    [self.yanzhengmaButton addTarget:self action:@selector(yanzhengmaButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.yanzhengmaTextField = [[UITextField alloc] init];
    self.yanzhengmaTextField.keyboardType=UIKeyboardTypeNumberPad;
    [self.yanzhengmaView addSubview:self.yanzhengmaTextField];
    [self.yanzhengmaTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.yanzhengmaView);
        make.left.equalTo(yanzhengmaLabel.mas_right).with.offset(15);
        make.right.equalTo(self.yanzhengmaButton.mas_left).with.offset(-15);
        
    }];
    self.yanzhengmaTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.yanzhengmaTextField.placeholder =@"请输入手机验证码";
    self.yanzhengmaTextField.font = [UIFont systemFontOfSize:14.f];
    self.yanzhengmaTextField.delegate = self;
    
}


//隐藏密码
- (void)jymimaButtonAction:(UIButton *)sender {
    
    if (self.jymimaTextField.secureTextEntry == YES) {
        
        self.jymimaTextField.secureTextEntry = NO;
        [self.yanjingButton setImage:[UIImage imageNamed:@"icon_yangjingh"] forState:UIControlStateNormal];
    }else {
        self.jymimaTextField.secureTextEntry = YES;
        [self.yanjingButton setImage:[UIImage imageNamed:@"icon_biyan_h"] forState:UIControlStateNormal];
        
    }
    
    
}

//扫码
- (void)siteButtonAction:(UIButton *)sender{
    
    XMRichScanViewController * RichScanVC = [[XMRichScanViewController alloc] init];
    RichScanVC.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:RichScanVC animated:YES];
  
    
}
//确认方法
- (void)querenButtonAction:(UIButton *)sender {
    
    if (![self.siteTextField.text isEqualToString:@""] && ![self.shuliangTextField.text isEqualToString:@""] && ![self.jymimaTextField.text isEqualToString:@""] && ![self.yanzhengmaTextField.text isEqualToString:@""]) {
        
        if (self.jymimaTextField.text.length != 6 ) {
            self.hud=[WKProgressHUD popMessage:@"交易密码长度必须为6个字符" inView:self.view duration:1 animated:YES];
            return;
        }
        if (self.yanzhengmaTextField.text.length != 6 ) {
            self.hud=[WKProgressHUD popMessage:@"验证码不正确" inView:self.view duration:1 animated:YES];
            return;
        }
        
        int user = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] intValue];
       
        if ([self.type isEqualToString:@"2"]) {
            
            NSDictionary * params = @{@"userId" : @(user),
                                      @"address" : self.siteTextField.text,
                                      @"amount" : self.shuliangTextField.text,
                                      @"sign" : self.yqsign,
                                      @"posId" : self.posid,
                                      @"orderNumber" : self.DorderNumber,
                                      @"password" : self.jymimaTextField.text,
                                      @"verifyCode" : self.yanzhengmaTextField.text
                                      
                                      };
            NSLog(@"pos===%@",params);
            self.querenButton.userInteractionEnabled=NO;
            [GPosAPI requestTransferAccountPosWithParams:params andCallback:^(id obj) {
                
                if ( obj == nil ){
                    self.querenButton.userInteractionEnabled=YES;
                    [SVProgressHUD showErrorWithStatus:[obj objectForKey:@"message"]];
                    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                }else {
                    
                    if ([[obj objectForKey:@"code"] intValue] == 200) {
                        [SVProgressHUD showSuccessWithStatus:@"发送成功"];
                        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                        [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                        
                        
                        NSNotification *LoseResponse = [NSNotification notificationWithName:@"buttonLoseResponse" object:nil];
                        
                        //                    2.发送通知:
                        [[NSNotificationCenter defaultCenter] postNotification:LoseResponse];
                        
                        
                        [self.navigationController popViewControllerAnimated:YES];
                        self.querenButton.userInteractionEnabled=YES;
                    }else if ([[obj objectForKey:@"code"] intValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402) {
                        
                        UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                        //storyboard里的ID
                        XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                        vc.navigationController.navigationBarHidden=NO;
                        //                    //设置当前窗口的根控制器
                        [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                        self.querenButton.userInteractionEnabled=YES;
                        [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                        [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                        
                    }else {
                        self.querenButton.userInteractionEnabled=YES;
                        self.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
                    }
                }
                
                
            }];
            
//            NSDictionary *params = @{@"userId" :@(user),
//                                     @"amount" : @([self.shuliangTextField.text floatValue]),
//                                     @"address" : self.siteTextField.text,
//                                     @"password" : self.jymimaTextField.text,
//                                     @"verifyCode" : self.yanzhengmaTextField.text
//                                     };
//            NSLog(@"sss--%@",params);
//            self.querenButton.userInteractionEnabled=NO;
//            [GPosAPI requestTransferAccountWithParams:params andCallback:^(id obj) {
//                NSLog(@"dizhi---%@",obj);
//                if ( obj == nil ){
//                    self.querenButton.userInteractionEnabled=YES;
//                    [SVProgressHUD showErrorWithStatus:[obj objectForKey:@"message"]];
//                    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
//                    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
//                }else {
//
//                    if ([[obj objectForKey:@"code"] intValue] == 200) {
//                        [SVProgressHUD showSuccessWithStatus:@"发送成功"];
//                        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
//                        [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
//
//
//                        NSNotification *LoseResponse = [NSNotification notificationWithName:@"buttonLoseRespon" object:nil];
//
//                        //                    2.发送通知:
//                        [[NSNotificationCenter defaultCenter] postNotification:LoseResponse];
//
//
//                        [self.navigationController popViewControllerAnimated:YES];
//                        self.querenButton.userInteractionEnabled=YES;
//                    }else if ([[obj objectForKey:@"code"] intValue] == 401) {
//
//                        UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
//                        //storyboard里的ID
//                        XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
//                        vc.navigationController.navigationBarHidden=NO;
//                        //                    //设置当前窗口的根控制器
//                        [UIApplication sharedApplication].keyWindow.rootViewController=vc;
//
//                        [SVProgressHUD showInfoWithStatus:@"您的账号已在其它设备登录,请重新登录!"];
//                        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
//                        [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
//                        self.querenButton.userInteractionEnabled=YES;
//
//                    }else {
//                        self.querenButton.userInteractionEnabled=YES;
//                        self.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
//                    }
//                }
//
//            }];
            
        }else {
            
            NSDictionary *params = @{@"userId" :@(user),
                                     @"amount" : @([self.shuliangTextField.text floatValue]),
                                     @"address" : self.siteTextField.text,
                                     @"password" : self.jymimaTextField.text,
                                     @"verifyCode" : self.yanzhengmaTextField.text
                                     };
            NSLog(@"sss--%@",params);
            self.querenButton.userInteractionEnabled=NO;
            [GPosAPI requestTransferAccountWithParams:params andCallback:^(id obj) {
                NSLog(@"dizhi---%@",obj);
                if ( obj == nil ){
                    self.querenButton.userInteractionEnabled=YES;
                    [SVProgressHUD showErrorWithStatus:[obj objectForKey:@"message"]];
                    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                }else {
                    
                    if ([[obj objectForKey:@"code"] intValue] == 200) {
                        [SVProgressHUD showSuccessWithStatus:@"发送成功"];
                        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                        [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                        
                        
                        NSNotification *LoseResponse = [NSNotification notificationWithName:@"buttonLoseRespon" object:nil];
                        
                        //                    2.发送通知:
                        [[NSNotificationCenter defaultCenter] postNotification:LoseResponse];
                        
                        
                        [self.navigationController popViewControllerAnimated:YES];
                        self.querenButton.userInteractionEnabled=YES;
                    }else if ([[obj objectForKey:@"code"] intValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402) {
                        
                        UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                        //storyboard里的ID
                        XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                        vc.navigationController.navigationBarHidden=NO;
                        //                    //设置当前窗口的根控制器
                        [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                        
                        [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                        [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                        self.querenButton.userInteractionEnabled=YES;
                        
                    }else {
                        self.querenButton.userInteractionEnabled=YES;
                        self.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
                    }
                }
                
            }];

//            NSDictionary * params = @{@"userId" : @(user),
//                                      @"address" : self.siteTextField.text,
//                                      @"amount" : self.shuliangTextField.text,
//                                      @"sign" : self.yqsign,
//                                      @"posId" : self.posid,
//                                      @"orderNumber" : self.DorderNumber,
//                                      @"password" : self.jymimaTextField.text,
//                                      @"verifyCode" : self.yanzhengmaTextField.text
//
//                                      };
//            NSLog(@"pos===%@",params);
//            self.querenButton.userInteractionEnabled=NO;
//            [GPosAPI requestTransferAccountPosWithParams:params andCallback:^(id obj) {
//
//                if ( obj == nil ){
//                    self.querenButton.userInteractionEnabled=YES;
//                    [SVProgressHUD showErrorWithStatus:[obj objectForKey:@"message"]];
//                    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
//                    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
//                }else {
//
//                    if ([[obj objectForKey:@"code"] intValue] == 200) {
//                        [SVProgressHUD showSuccessWithStatus:@"发送成功"];
//                        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
//                        [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
//
//
//                        NSNotification *LoseResponse = [NSNotification notificationWithName:@"buttonLoseResponse" object:nil];
//
//                        //                    2.发送通知:
//                        [[NSNotificationCenter defaultCenter] postNotification:LoseResponse];
//
//
//                        [self.navigationController popViewControllerAnimated:YES];
//                        self.querenButton.userInteractionEnabled=YES;
//                    }else if ([[obj objectForKey:@"code"] intValue] == 401) {
//
//                        UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
//                        //storyboard里的ID
//                        XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
//                        vc.navigationController.navigationBarHidden=NO;
//                        //                    //设置当前窗口的根控制器
//                        [UIApplication sharedApplication].keyWindow.rootViewController=vc;
//                        self.querenButton.userInteractionEnabled=YES;
//                        [SVProgressHUD showInfoWithStatus:@"您的账号已在其它设备登录,请重新登录!"];
//                        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
//                        [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
//
//                    }else {
//                        self.querenButton.userInteractionEnabled=YES;
//                        self.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
//                    }
//                }
//
//
//            }];
//
        }
       

    }else if ([self.siteTextField.text isEqualToString:@""] ) {
        
        self.hud=[WKProgressHUD popMessage:@"钱包地址不能为空" inView:self.view duration:1 animated:YES];
        NSLog(@"钱包地址不能为空");
        
    }else if (![self.siteTextField.text isEqualToString:@""] && [self.shuliangTextField.text isEqualToString:@""] ) {
         self.hud=[WKProgressHUD popMessage:@"GPOS数量不能为空" inView:self.view duration:1 animated:YES];
        NSLog(@"GPOS数量不能为空");
        
    }else if (![self.siteTextField.text isEqualToString:@""] && ![self.shuliangTextField.text isEqualToString:@""] && [self.jymimaTextField.text isEqualToString:@""]) {
        NSLog(@"交易密码不能为空");
        self.hud=[WKProgressHUD popMessage:@"交易密码不能为空" inView:self.view duration:1 animated:YES];
        
        
    }else if (![self.siteTextField.text isEqualToString:@""] && ![self.shuliangTextField.text isEqualToString:@""] && ![self.jymimaTextField.text isEqualToString:@""] && [self.yanzhengmaTextField.text isEqualToString:@""]) {
        
        self.hud=[WKProgressHUD popMessage:@"验证码不能为空" inView:self.view duration:1 animated:YES];
        NSLog(@"验证码不能为空");
        
    }
    
}

// 验证码
- (void)yanzhengmaButtonAction:(EBCountDownButton *)sender {
    
    [self.yanzhengmaButton countDownWithDuration:180 completion:^(BOOL finished) {
        NSLog(@"finished");
    }];
    NSDictionary * para = @{@"phone" : self.phonetextlabel.text,
                            @"type" : @(6)
                            };
    
    [GPosAPI requestSendCodeWithParams:para andCallback:^(id obj) {
        NSLog(@"--%@",obj);
        
        if ( obj == nil )
        {
         
            self.hud=[WKProgressHUD popMessage:@"请求失败" inView:self.view duration:1 animated:YES];
            
        }else{
            
            if ([[obj objectForKey:@"code"] intValue] == 200) {
                self.hud=[WKProgressHUD popMessage:@"发送短信验证码成功" inView:self.view duration:1 animated:YES];
                
              
            }else if ([[obj objectForKey:@"code"] intValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402){
                
                UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                //storyboard里的ID
                XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                vc.navigationController.navigationBarHidden=NO;
                //                    //设置当前窗口的根控制器
                [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                
                [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }else{
                
                 self.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
               
                
            }
            
            
        }
        
        
    }];
    
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.jymimaTextField ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.jymimaTextField.text.length >= 6) {
            self.jymimaTextField.text = [textField.text substringToIndex:6];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        NSString *allstr=[textField.text stringByReplacingCharactersInRange:range withString:string];
        if (self.jymimaTextField.isSecureTextEntry ==YES)
        {
            self.jymimaTextField.text=allstr;
            return NO;
        }
    }
    
    if (textField == self.yanzhengmaTextField ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.yanzhengmaTextField.text.length >= 6) {
            self.yanzhengmaTextField.text = [textField.text substringToIndex:6];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        NSString *allstr=[textField.text stringByReplacingCharactersInRange:range withString:string];
        if (self.yanzhengmaTextField.isSecureTextEntry ==YES)
        {
            self.yanzhengmaTextField.text=allstr;
            return NO;
        }
    }
    
    if (textField == self.shuliangTextField ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.shuliangTextField.text.length >= 10) {
            self.shuliangTextField.text = [textField.text substringToIndex:10];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        if ([string isEqualToString:@"."] && ([textField.text rangeOfString:@"."].location != NSNotFound || [textField.text isEqualToString:@""])) {
            return NO;
        }
        
        NSMutableString *str = [[NSMutableString alloc] initWithString:textField.text];
        [str insertString:string atIndex:range.location];
        if (str.length >= [str rangeOfString:@"."].location+6){
            return NO;
        }
        
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUM] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        return [string isEqualToString:filtered];
        
    }
    
    return YES;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed=YES;
    }
    return self;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
